package com.idealighter.game.dictionary.dic;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.core.DictionaryTable;
import com.idealighter.game.dictionary.domain.DoudizhuMatchRoomDomain;
import com.idealighter.game.dictionary.domwrapper.DoudizhuMatchRoomDomWrapper;
import com.idealighter.game.dictionary.mapper.DoudizhuMatchRoomMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Singleton
public class DoudizhuMatchRoomDic implements DictionaryTable<DoudizhuMatchRoomDomWrapper> {

  private DoudizhuMatchRoomMapper mapper;
  private List<DoudizhuMatchRoomDomWrapper> list;
  private Map<Integer, DoudizhuMatchRoomDomWrapper> map;

  @Inject
  public DoudizhuMatchRoomDic(DoudizhuMatchRoomMapper mapper) {
    this.mapper = mapper;
    load();
  }

  @Override
  public List<DoudizhuMatchRoomDomWrapper> list() {
    list = new ArrayList<>();
    return list;
  }

  @Override
  public Map<Integer, DoudizhuMatchRoomDomWrapper> map() {

    return map;
  }

  @Override
  public void load() {
    List<DoudizhuMatchRoomDomain> doms = mapper.selectAll();
    List<DoudizhuMatchRoomDomWrapper> domWrappers = new ArrayList<>();
    Map<Integer, DoudizhuMatchRoomDomWrapper> map = new HashMap<>();

    for (DoudizhuMatchRoomDomain dom : doms) {
      DoudizhuMatchRoomDomWrapper wrapper = new DoudizhuMatchRoomDomWrapper(dom);
      domWrappers.add(wrapper);
      map.put(dom.getId(), wrapper);
    }

    this.list = domWrappers;
    this.map = map;
  }

  @Override
  public DoudizhuMatchRoomDomWrapper get(int id) {

    return map.get(id);
  }
}
